package hanjia;

import suanfa.ListNode;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/5-下午3:12
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode234 {

    public ListNode middleNode(ListNode head) {
        ListNode p1=head;
        ListNode p2=head;

        while (p2!=null&&p2.next!=null){
            p1=p1.next;
            p2=p2.next;
            p2=p2.next;
        }
        return p1;


    }

    public  ListNode reverseNode(ListNode node) {
        if (node == null) {
            return null;
        }

        ListNode temp = node;
        ListNode pre = null;
        while (temp != null) {

            ListNode next=temp.next;
            temp.next=pre;
            pre=temp;
            temp=next;

        }
        return pre;
    }
    public boolean isPalindrome(ListNode head) {
      if (head.next==null){
          return true;
      }
        ListNode afterNode = middleNode(head);
        ListNode reversedNode = reverseNode(afterNode);
        ListNode temp=reversedNode;
        while (temp!=null){
            if (temp.val!=head.val){
                return false;
            }

            temp=temp.next;
            head=head.next;

        }

return  true;
    }
}
